#include "zhuchangjing.h"
#include<QPainter>
#include<QMouseEvent>
#include<QDebug>
#include<ctime>
#include<QVBoxLayout>


zhuchangjing::zhuchangjing(QWidget *parent) : QWidget(parent)
{
    this->setFixedSize(500,800);
    this->setWindowTitle("飞机大战");
    mtimer.setInterval(10);//定时器刷新时间10ms
    //playgame();
    //随机数种子
    srand((unsigned)time(NULL));
    score=new QLabel(this);
    score->raise();
    score->setFont(QFont("Algerian",10));
    QVBoxLayout *lay=new QVBoxLayout;
    lay->addWidget(score);
    setLayout(lay);
    scorenum=0;
}

void zhuchangjing::playgame()
{
    //启动定时器
    mtimer.start();
    connect(&mtimer,&QTimer::timeout,[=]{
        //碰撞检测
        collision();
        //敌机出场
        enemyToScene();
        //更新游戏中元素的坐标
        updatePosition();
        //绘制到屏幕中
        update();
    });
}
void zhuchangjing::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.drawPixmap(0,mmap.map1y,mmap.map1);
    painter.drawPixmap(0,mmap.map2y,mmap.map2);
    //绘制飞机
    painter.drawPixmap(hero.mx,hero.my,hero.plane);
    //绘制子弹
    for(int i=0;i<30;i++)
    {
        if(hero.mbullets[i].mfree==false)
        {
            painter.drawPixmap(hero.mbullets[i].mx,hero.mbullets[i].my,hero.mbullets[i].mbullet);
        }
    }
    //绘制敌机
    for(int i=0;i<20;i++)
    {
        if(enemies[i].mfree==false)
        {
            painter.drawPixmap(enemies[i].mx,enemies[i].my,enemies[i].menemy);
            for(int j=0;j<30;j++)
            {
                if(enemies[i].ebullets[j].mfree==false)
                {
                    painter.drawPixmap(enemies[i].ebullets[j].mx,enemies[i].ebullets[j].my,enemies[i].ebullets[j].ebullet);
                }
            }
        }
        else if(enemies[i].mfree==true)
        {
            for(int j=0;j<30;j++)
            {
                if(enemies[i].ebullets[j].mfree==false)
                {
                    painter.drawPixmap(enemies[i].ebullets[j].mx,enemies[i].ebullets[j].my,enemies[i].ebullets[j].ebullet);
                }
            }
        }

    }
    //绘制爆炸
    for(int i=0;i<20;i++)
    {
        if(bombs[i].mfree==false)
        {
            painter.drawPixmap(bombs[i].mx,bombs[i].my,bombs[i].mpixarr[bombs[i].mindex]);
        }
    }
}
void zhuchangjing::mouseMoveEvent(QMouseEvent *e)
{
     int x=e->x()-hero.mrect.width()*0.5;
     int y=e->y()-hero.mrect.height()*0.5;
     if(x<=0)
     {
         x=0;
     }
     if(x>=500-hero.mrect.width())
     {
         x=500-hero.mrect.width();
     }
     if(y<=0)
     {
         y=0;
     }
     if(y>=800-hero.mrect.height())
     {
         y=800-hero.mrect.height();
     }
     hero.setPosition(x,y);
}

void zhuchangjing::updatePosition()
{
    //更新地图坐标
    mmap.mapPosition();
    score->setText(QString("分数：%1").arg(scorenum));
    //发射子弹
    hero.shoot();
    //计算所有非空闲子弹的当前坐标
    for(int i=0;i<30;i++)
    {
        if(hero.mbullets[i].mfree==false)
        {
            hero.mbullets[i].updatePosition();
        }
    }
    //计算敌机坐标
    for(int i=0;i<20;i++)
    {
        if(enemies[i].mfree==false)
        {
            enemies[i].updatePosition();
            enemies[i].shoot();
        }
        for(int j=0;j<30;j++)
        {
            if(enemies[i].ebullets[j].mfree==false)
            {
                enemies[i].ebullets[j].updatePositionEn();
            }
        }


    }
    //计算爆炸的图片
    for(int i=0;i<20;i++)
    {
        if(bombs[i].mfree==false)
        {
            bombs[i].updateInfo();
        }
    }
}

void zhuchangjing::enemyToScene()
{
    mrecorder++;
    if(mrecorder<50)
    {
        return;
    }
    mrecorder=0;
    for(int i=0;i<20;i++)
    {
        if(enemies[i].mfree==true)
        {
            enemies[i].mfree=false;
            enemies[i].mx=rand()%(500-enemies[i].mrect.width());
            enemies[i].my=-enemies[i].mrect.height();
            break;
        }
    }
}

void zhuchangjing::collision()
{
    //遍历我方子弹碰撞敌方飞机
    //遍历所有非空闲敌机
    for(int i=0;i<20;i++)
    {
        if(enemies[i].mfree)
        {
            continue;
        }
        //遍历所有非空闲子弹
        for(int j=0;j<30;j++)
        {
            if(hero.mbullets[j].mfree)
            {
                continue;
            }
            //如果子弹和飞机相交，发生碰撞，同时将子弹和敌机状态设为真
            if(enemies[i].mrect.intersects(hero.mbullets[j].mrect))
            {
                enemies[i].mfree=true;
                hero.mbullets[j].mfree=true;
                for(int j=0;j<30;j++)
                {
                    if(enemies[i].ebullets[j].mfree==false)
                    {
                        enemies[i].ebullets[j].updatePositionEn();
                    }
                }
                scorenum+=10;
                if(scorenum==500)
                {
                    winning();
                    mtimer.stop();
                    update();
                }
                //播放爆炸
                for(int k=0;k<20;k++)
                {
                    if(bombs[k].mfree)
                    {
                        bombs[k].mfree=false;
                        bombs[k].mx=enemies[i].mx;
                        bombs[k].my=enemies[i].my;
                        break;
                    }
                }
            }
        }
        //双方飞机碰撞，游戏失败
        if(enemies[i].mrect.intersects(hero.mrect))
        {
            enemies[i].mfree=true;
            gameover();
            mtimer.stop();
            update();
        }
    }
}



void zhuchangjing::gameover()
{
    e=new endgame;
    e->show();
    this->close();
}

void zhuchangjing::winning()
{
    w=new wingame;
    w->show();
    this->close();
}
